package org.mock.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.mock.dto.SabreHotelDetailDTO;
import org.mock.entity.SabreHotelAvail;
import org.mock.entity.SabreHotelDetail;
import org.mock.enums.SabreMockStateEnum;
import org.mock.mapper.SabreHotelAvailMapper;
import org.mock.service.SabreHotelAvailService;
import org.mock.dto.SabreHotelAvailDTO;
import org.mock.common.Condition;
import org.mock.service.SabreMockSceneService;
import org.mock.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import cn.hutool.core.util.StrUtil;

import java.util.List;

/**
 * 服务实现类
 *
 * @author AI
 * @since 2025-05-20
 */
@Service
@AllArgsConstructor
@Slf4j
public class SabreHotelAvailServiceImpl implements SabreHotelAvailService {

    protected SabreHotelAvailMapper sabreHotelAvailMapper;
    private SabreMockSceneService sabreMockSceneService;

    @Override
    public SabreHotelAvailDTO handleRequest(JSONObject request) {
        log.info("[getHotelAvail-sabre] 请求：{}", request);
        JSONObject requestJson = request.getJSONObject("GetHotelAvailRQ");
        JSONObject searchCriteria = requestJson.getJSONObject("SearchCriteria");
        JSONObject hotelRefs = searchCriteria.getJSONObject("HotelRefs");
        //到这儿就成数组了
        JSONObject hotelRef = hotelRefs.getJSONArray("HotelRef").getJSONObject(0);
        String hotelCode = hotelRef.get("HotelCode").toString();
        log.info("hotelCode:{}", hotelCode);

        SabreHotelAvailDTO dto = new SabreHotelAvailDTO();
        dto.setHotelCode(hotelCode);
        return dto;
    }

    @Override
    public SabreHotelAvailDTO handleQueryWrapper(SabreHotelAvailDTO dto) {
        String currentMockNo = sabreMockSceneService.getMockNoByStatus(SabreMockStateEnum.ON_REQUEST.getValue());

        if ("000".equals(currentMockNo)) {
            log.info("当前查询模式：忽略所有的查询条件，直接给一个新的dto");
            dto = new SabreHotelAvailDTO();
        } else {
            log.info("使用传入的查询条件dto: {}", dto);
        }
        return dto;
    }

    @Override
    public String handleData(SabreHotelAvail data) {
        if (ObjectUtil.isEmpty(data)) {
            log.info("暂无数据");
            return StrUtil.EMPTY;
        }

        //修改响应体中的hotel_code，改成传入的那个


        return data.getResponse();
    }

    @Override
    public SabreHotelAvail getOne(SabreHotelAvailDTO dto) {
        List<SabreHotelAvail> list = sabreHotelAvailMapper.selectList(Condition.getQueryWrapper(BeanCopyUtils.copy(dto, SabreHotelAvail.class)));
        if (CollUtil.isEmpty(list)) {
            return null;
        } else {
            return list.get(0);
        }
//        return sabreHotelAvailMapper.selectOne(Condition.getQueryWrapper(BeanCopyUtils.copy(dto, SabreHotelAvail.class)));
    }
}
